<template>
  <div class="register-form">
    <h2>用户注册</h2>
    <el-form :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-button type="primary" @click="handleRegister">注册</el-button>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useUserStore } from '@/stores/user';
import { useRouter } from 'vue-router';

const form = ref({
  username: '',
  password: '',
  email: ''
});

const userStore = useUserStore();
const router = useRouter();

const handleRegister = async () => {
  try {
    await userStore.register(form.value);
    router.push('/');
  } catch (error) {
    console.error('注册失败', error);
  }
};
</script>